package me.sauce.folkpocket48.video

import android.content.Context
import android.content.Intent
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import com.orhanobut.logger.Logger
import me.sauce.folkpocket48.base.BaseActivity
import tv.danmaku.ijk.media.player.IjkMediaPlayer

/**
 * @author sauceWu
 * @since 2018/2/22 16:39
 */

class VideoActivity : BaseActivity() {
    lateinit var videoView: SurfaceView

    override fun injectView(): View {
        videoView = SurfaceView(this)
        videoView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        return videoView
    }

    override fun initView() {
        super.initView()

        videoView.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceDestroyed(p0: SurfaceHolder?) {
            }

            override fun surfaceCreated(p0: SurfaceHolder?) {
                val ijkMediaPlayer = IjkMediaPlayer()
                ijkMediaPlayer.setLogEnabled(true)
                ijkMediaPlayer.dataSource = intent.getStringExtra("url")
                ijkMediaPlayer.setDisplay(videoView.holder)
                ijkMediaPlayer.setOnTimedTextListener { iMediaPlayer, ijkTimedText -> Logger.d(ijkTimedText) }
                ijkMediaPlayer.prepareAsync()
                ijkMediaPlayer.start()
            }

            override fun surfaceChanged(p0: SurfaceHolder?, p1: Int, p2: Int, p3: Int) {
            }
        })
    }

    companion object {
        fun start(context: Context, url: String) {
            val starter = Intent(context, VideoActivity::class.java)
            starter.putExtra("url", url)
            context.startActivity(starter)
        }
    }
}
